import config from "@/config.js"
import * as util from "@/common/js/util.js"
import baseRequest from "@/common/js/request.js"

/**
 * 上传图片
 * @param fileList 例子： ['图片1 URL', '图片2 URL']
 * @returns {Promise<unknown[]>}
 */
function startUpload(fileList) {
	const uploadUrl = config.base_url + "/jcApi/upload/start"
	const uploadList = fileList.map((item) => util.httpUpload(uploadUrl, item))
	return Promise.all(uploadList)
}

/**
 * 开始简单上传
 * @param files
 */
async function startEasyUpload(files = []) {
	let doneList = []
	let fileList = []
	files.forEach((item) => {
		if (item.status === 'done') {
		  doneList.push(item.id)
		} else {
		  fileList.push(item.url)
		}
	})
	if (fileList) {
		try {
			const uploadList = (await startUpload(fileList)) || []
			doneList = doneList.concat(uploadList.map((item) => item.data))
		} catch (e) {
			console.log(e)
		}
	}
	const fileIds = doneList.join(',')
	return fileIds
}

function getFileList (businessId) {
	return baseRequest('/jc/file/business/' + businessId)
}

export{
	getFileList,
	startEasyUpload
}